import { Card, Col, message, Row } from 'antd';
import { connect } from 'umi';
import cls from 'classnames';
import { AnnounceUpdateContext } from '../common/UpdateContext';
import { UpdateViewLeft } from '../mview/UpdateViewLeft';
import '../common/styles.less';
import { AnnounceManageAPI } from '../common/type';
import { useUpdateReducer } from '../common/UpdateReducer';
import { UpdateViewRight } from './UpdateViewRight';
import { getAnnounceByIDService } from '../service';
import { useEffect, useState } from 'react';

/** 定义公告新增和编辑视图和业务逻辑 */
const AnnounceUpdateView: React.FC<AnnounceManageAPI.IAnnounceUpdateView> = ({
  curRes,
  locationProps,
}) => {
  /**初始化公告业务标签类名前缀 */
  const prefixCls = 'announce-update-';

  const {
    dispatch,
    leftSelectRows,
    leftSelectKeys,
    leftExpandedKeys,
    leftAutoExpandParent,
  } = useUpdateReducer();

  const [announceInfo, setAnnounceInfo] = useState<any>();

  /**
   * 获取数据详情
   * @param res
   */
  const getAnnounceByIDCall = (res: any) => {
    if (res && res?.msgFlag) {
      setAnnounceInfo(res?.obj);
    } else {
      message.error(res?.msg);
    }
  };

  const { getAnnounceInfoRun, getAnnounceLoading } = getAnnounceByIDService(
    getAnnounceByIDCall,
  );

  useEffect(() => {
    if (locationProps?.state?.id) {
      let infoPayload = {
        ...locationProps?.state,
      };
      getAnnounceInfoRun(infoPayload);
    }
  }, [locationProps?.state]);

  /**
   * 刷新详情
   */
  const reloadAnnounceInfo = () => {
    let infoPayload = {
      ...locationProps?.state,
    };
    getAnnounceInfoRun(infoPayload);
  };

  return (
    <AnnounceUpdateContext.Provider
      value={{
        prefixCls,
        curRes,
        dispatch, //派发
        leftSelectRows,
        leftSelectKeys,
        leftExpandedKeys,
        leftAutoExpandParent,
        announceInfo,
        locationProps,
        reloadAnnounceInfo,
      }}
    >
      <Row className={cls(prefixCls + 'row')} gutter={[8, 8]}>
        <Col span={16} className={cls(prefixCls + 'row-col')}>
          <UpdateViewLeft getAnnounceLoading={getAnnounceLoading} />
        </Col>
        <Col span={8} className={cls(prefixCls + 'row-col')}>
          <UpdateViewRight />
        </Col>
      </Row>
    </AnnounceUpdateContext.Provider>
  );
};

export default connect(({ baseLogin, navTab }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
  navTab,
}))(AnnounceUpdateView);
